package com.baizhi.wxt.service;

import com.baizhi.wxt.dao.CategoryMapper;
import com.baizhi.wxt.dto.CategoryPageDTO;
import com.baizhi.wxt.entity.Category;
import com.baizhi.wxt.entity.CategoryExample;
import com.baizhi.wxt.vo.CategoryVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryVo queryOnePage(int page, int pageSize) {
        //一级类别分页查
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample,rowBounds);

        //查询总共行数
        int totalCount = categoryMapper.selectCountByExample(categoryExample);

        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setPage(page);
        categoryVo.setTotal(totalCount);
        categoryVo.setRows(categoryList);

        return categoryVo;
    }

    //二级类别分页查
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CategoryVo queryTwoPage(int page, int pageSize,int categoryId) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(categoryId);
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(categoryExample, rowBounds);

        //查总行数
        int totalCount = categoryMapper.selectCountByExample(categoryExample);
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setPage(page);
        categoryVo.setTotal(totalCount);
        categoryVo.setRows(categoryList);

        return categoryVo;
    }


    //删除
    @Override
    public Map<String,Object> delete(Category category) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {

            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andIdEqualTo(category.getId());
            if (category.getParentId() != null) {
                //删除二级类别
                categoryMapper.deleteByExample(categoryExample);
                map.put("message", "删除成功");
                map.put("status", 200);
                return map;
            } else {
                //根据一级类别 查询二级类别
                CategoryExample example = new CategoryExample();
                example.createCriteria().andParentIdEqualTo(category.getId());
                int towCount = categoryMapper.selectCountByExample(example);
                if (towCount > 0) {
                    map.put("message", "一级类别下有二级类别不能删除");
                    map.put("status", 400);
                } else {
                    categoryMapper.deleteByExample(categoryExample);
                    map.put("message", "删除成功");
                    map.put("status", 200);
                }
                return map;
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("message", "删除失败");
            map.put("status", 400);
            return map;
        }
    }


    //添加
    @Override
    public Map<String,Object> add(Category category) {
        Map<String,Object> map = new HashMap<>();
        //添加
        try{
            if(category.getParentId()==null) {
                category.setLevels(1);
            }else {
                category.setLevels(2);

            }
            category.setCateName(category.getCateName());
            category.setParentId(category.getParentId());
            categoryMapper.insert(category);
            map.put("message", "添加成功");
            map.put("status", 200);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message", "添加失败");
            map.put("status", 400);
        }
        return map;
    }

    //根据id查一个
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Category queryById(int id) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(id);
        Category category = categoryMapper.selectOneByExample(categoryExample);
        return category;
    }

    //修改
    @Override
    public Map<String, Object> update(Category category) {
        Map<String,Object> map = new HashMap<>();

        try {

            category.setId(category.getId());
            category.setCateName(category.getCateName());
            category.setParentId(category.getParentId());
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andIdEqualTo(category.getId());
            categoryMapper.updateByExampleSelective(category,categoryExample);
            map.put("message", "修改成功");
            map.put("status", 200);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message", "修改失败");
            map.put("status", 400);
            return map;
        }
    }


    @Override
    //查所有一级类别
    public List<Category> queryByLevelsCategory(int levels){
        CategoryVo categoryVo = new CategoryVo();

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        return categoryList;
    }

}
